package it.softphone.cmc.impl;

import it.softphone.cmc.CMCServiceManager;
import it.softphone.cmc.CMCServiceManager.JerseyCall;
import it.softphone.cmc.server.service.QueryTask;
import it.softphone.cmc.server.service.SystemManagerService;
import it.softphone.cmc.server.service.exception.CMCException;
import it.softphone.cmc.shared.entity.Account;
import it.softphone.cmc.shared.entity.Account.Preference;
import it.softphone.cmc.shared.entity.Attributes;
import it.softphone.cmc.shared.entity.Tenant;
import it.softphone.cmc.shared.entity.TenantUser;
import it.softphone.cmc.shared.entity.Transaction;
import it.softphone.cmc.shared.entity.Version;
import it.softphone.commons.json.server.JSONException;
import it.softphone.commons.json.server.JSONObject;
import it.softphone.commons.json.server.JSONUtils;
import it.softphone.commons.service.LogAware;

import java.util.List;

import javax.ws.rs.core.MediaType;

import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanCodex;

public class SystemServiceClientImpl implements SystemManagerService, LogAware {

	final private CMCServiceManager manager;
	
	
	public SystemServiceClientImpl(CMCServiceManager manager) {
		super();
		this.manager = manager;
		
		JSONUtils.registerConverter( Attributes.class, new JSONAutoBeanAttributesConverter(this) );
		
	}
	
	@Override
	public <T> void find(String sqlCommand, Object paramMap, QueryTask<List<T>> task) throws CMCException {
		// TODO Auto-generated method stub

	}

	@Override
	public Object findSingle(String sqlCommand, Object paramMap) throws CMCException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <R> R execNewTransaction(String method, EMTask<R> task) throws CMCException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Version getLastVersion(String version) throws CMCException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Attributes makeAttributes() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Attributes makeAttributesFromJson(JSONObject object)
			throws JSONException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Transaction makeTransactionFromJson(JSONObject object)
			throws JSONException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Transaction makeTransaction() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void createTransaction(Transaction transaction) throws CMCException {
		// TODO Auto-generated method stub

	}

	@Override
	public void storeTransaction(Transaction transaction) throws CMCException {
		// TODO Auto-generated method stub

	}

	@Override
	public Tenant makeTenantFromJson(JSONObject object) throws JSONException {
		
		Tenant t = makeTenant();
		
		return JSONUtils.copyTo(t, object);
	}

	@Override
	public Tenant makeTenant() {
		AutoBean<Tenant> aa = AutoBeanCodex.decode(manager.ef, Tenant.class, "{}");

		return aa.as();

		// return manager.ef.makeTenant().as(); // return null

		//return AutoBeanFactorySource.createBean(Tenant.class, manager.configuration).as(); // return null
	
	}

	@Override
	public boolean createTenant(Tenant tenant) throws CMCException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void storeTenant(Tenant tenant) throws CMCException {

		final it.softphone.commons.json.server.JSONObject o = new it.softphone.commons.json.server.JSONObject(tenant);
		
		String result = manager.call( new JerseyCall<String>() {

			@Override
			public String exec() {
								
				return manager.getClient()
						.resource(manager.getServiceUrl("tenant"))
						.type(MediaType.APPLICATION_JSON)
						.put(String.class, o.toString());
								
								
						
				
			}
			
		});
		
		logger.info( "storeTenant\n\t" + result );
		
		//AutoBean<Tenant> aa = AutoBeanCodex.decode(manager.ef, Tenant.class, result);
		//return aa.as();
	}

	@Override
	public void deleteTenant(Tenant tenant) throws CMCException {
		// TODO Auto-generated method stub

	}

	@Override
	public void findAllTenants(QueryTask<List<Tenant>> task)
			throws CMCException {
		// TODO Auto-generated method stub

	}

	@Override
	public Tenant findTenantById(final String nameOfId, final String valueOfId) throws CMCException {
		
		String result = manager.call( new JerseyCall<String>() {

			@Override
			public String exec() {
								
				return manager.getClient().resource(manager.getServiceUrl("tenant"))
						.queryParam("id", nameOfId)
						.queryParam("value", valueOfId)
						.get(String.class);
				
			}
			
		});
		
		logger.info( "findTenantById\n\t " + result );
		
		AutoBean<Tenant> aa = AutoBeanCodex.decode(manager.ef, Tenant.class, result);
		return aa.as();
	}

	@Override
	public Tenant findSystemTenant() throws CMCException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Account makeAccountFromJson(JSONObject object) throws JSONException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Account makeAccount() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Account findAccountById(String id) throws CMCException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String requestAccountCreation(String email, String password,
			String deviceId, String requestUrl) throws CMCException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Account confirmAccountCreation(String requestToken)
			throws CMCException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Account createAccount(Account account, String password,
			boolean activeRegistration) throws CMCException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void storeAccount(Account account) throws CMCException {
		// TODO Auto-generated method stub

	}

	@Override
	public Preference makeAccountPreference() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void findAccountPreferences(Account account,
			QueryTask<List<Preference>> task) throws CMCException {
		// TODO Auto-generated method stub

	}

	@Override
	public void createAccountPreference(Account account, Preference preference)
			throws CMCException {
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteAccountPreferenceByTenant(Account Account, Tenant tenant)
			throws CMCException {
		// TODO Auto-generated method stub

	}

	@Override
	public TenantUser makeUserFromJson(JSONObject object) throws JSONException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public TenantUser makeUser() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public TenantUser createUser(TenantUser user, Account account,
			boolean enable) throws CMCException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void storeUser(TenantUser user, boolean enable) throws CMCException {
		// TODO Auto-generated method stub

	}

	@Override
	public TenantUser findUserByAccount(Account account) throws CMCException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void activeRegistration(Account account) throws CMCException {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isRegistrationActive(Account account) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isRequestTokenExpired(String registrationToken) {
		// TODO Auto-generated method stub
		return false;
	}

}
